import java.awt.event.ActionListener;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class InvokeInterface1 {
	
	// အသုံးပြုမည့် Script စာသား
    public final static String SCRIPT
    = "function actionPerformed(event) {"
    + "    print('Button Pressed\\n' + event + '\\n\\n');"
    + "}";

    public InvokeInterface1() {
        // ScriptEngineManager ကို ခေါ်ယူခြင်း
        ScriptEngineManager manager = new ScriptEngineManager();
        // JavaScript သုံး ScriptEngine ၏ instance ကို ခေါ်ယူခြင်း
        ScriptEngine engine = manager.getEngineByName("js");
 
        try {
            // Script စာသားကို ဖက်ယူခြင်း
            engine.eval(SCRIPT);
            
            // ActionListener Interface ကို ခေါ်ယူခြင်း
            ActionListener listener 
                = ((Invocable)engine).getInterface(ActionListener.class);

            showFrame(listener);
        } catch (ScriptException ex) {
            System.err.println(ex.getMessage());
            ex.printStackTrace();
        }
    }
    
    private void showFrame(final ActionListener listener) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame("Test");
 
				JButton button = new JButton("OK");
                button.addActionListener(listener);
 
                frame.add(button);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }

    public static void main(String [] args) {
    	new InvokeInterface1();
    }
} 
